<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialog" width="50%" :close-on-click-modal="false" close-on-press-escape
      @close="closeHandle">
      <el-form ref="ruleForm" :model="obj" :rules="rules" label-width="140px">
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="企业" prop="entSpaceId">
              <el-select v-model="obj.entSpaceId" placeholder="请选择" clearable @change="entChange">
                <el-option v-for="ent in enterpriseData" :key="ent.id" :label="ent.name" :value="ent.spaceId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目" prop="spaceId">
              <el-select v-model="obj.spaceId" placeholder="请先选择企业" clearable>
                <el-option v-for="project in projectData" :key="project.id" :label="project.name"
                  :value="project.spaceId" />
              </el-select>
              <span style="cursor: pointer" @click="viewHotPlan">查看动火方案</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="编号" prop="serialNumber">
              <el-input v-model="obj.serialNumber" placeholder="请输入编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="动火作业单位" prop="hotWorkUnit">
              <el-input v-model="obj.hotWorkUnit" placeholder="请输入动火作业单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="动火级别" prop="hotLevel">
              <el-input v-model="obj.hotLevel" placeholder="请输入动火级别" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产运行部门" prop="productionDepartment">
              <el-input v-model="obj.productionDepartment" placeholder="请输入生产运行部门" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="动火时间" prop="hotTime">
              <wise-date v-model="obj.hotTime" type="datetime" format="yyyy-MM-dd HH:mm:ss" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="动火监督部门" prop="supervisionDepartment">
              <el-input v-model="obj.supervisionDepartment" placeholder="请输入动火监督部门" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="天气情况" prop="weather">
              <el-input v-model="obj.weather" placeholder="请输入天气情况" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item prop="name">
          <div>检查内容</div>
          <div class="smallTitle">动火作业：</div>
          <ul class="itemCon">
            <li v-for="item in hotWorkData" :key="item.code">{{ item.sort }}、{{ item.name }}</li>
          </ul>

          <div class="smallTitle">生产运行：</div>
          <ul class="itemCon">
            <li v-for="item in productionOperationData" :key="item.code">
              {{ hotWorkData.length + item.sort }}、{{ item.name }}</li>
          </ul>
          <div class="smallTitle">动火监督：</div>
          <ul class="itemCon">
            <li v-for="item in hotWorkSupervisionData" :key="item.code">
              {{ hotWorkData.length + productionOperationData.length + item.sort }}、{{ item.name }}
            </li>
          </ul>
        </el-form-item>
        <el-row :gutter="50">
          <el-col :span="12">
            <el-form-item label="动火作业负责人" prop="hotWorkUserId">
              <el-select v-model="obj.hotWorkUserId" placeholder="请选择" clearable :disabled="obj.id">
                <el-option v-for="member in entMemberData" :key="member.id" :label="member.username"
                  :value="member.userId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产运行负责人" prop="productionRunUserId">
              <el-select v-model="obj.productionRunUserId" placeholder="请选择" clearable :disabled="obj.id">
                <el-option v-for="member in entMemberData" :key="member.id" :label="member.username"
                  :value="member.userId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="动火监督负责人" prop="hotSupervisionUserId">
              <el-select v-model="obj.hotSupervisionUserId" placeholder="请选择" clearable :disabled="obj.id">
                <el-option v-for="member in entMemberData" :key="member.id" :label="member.username"
                  :value="member.userId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="动火现场指挥" prop="hotCommandUserId">
              <el-select v-model="obj.hotCommandUserId" placeholder="请选择" clearable :disabled="obj.id">
                <el-option v-for="member in entAdminData" :key="member.id" :label="member.username"
                  :value="member.userId" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="obj.remark" type="textarea" :rows="5" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>

    <hot-plan-table ref="hotPlanTable" />
  </div>
</template>

<script>
  import {
    postHandle
  } from '@/api'
  import wiseDate from '@/components/Crud/components/wiseDate'
  import HotPlanTable from './HotPlanTable.vue'
  import {
    mixin
  } from './inspectionSheet'
  export default {
    components: {
      wiseDate,
      HotPlanTable
    },
    mixins: [mixin],
    data() {
      return {
        dialog: false,
        title: '添加',
        obj: {
          entSpaceId: null,
          spaceId: null,
          serialNumber: null,
          hotWorkUnit: null,
          hotLevel: null,
          productionDepartment: null,
          hotTime: null,
          supervisionDepartment: null,
          weather: null,
          remark: null,
          hotWorkUserId: null,
          productionRunUserId: null,
          hotSupervisionUserId: null,
          hotCommandUserId: null
        },
        rules: {
          entSpaceId: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }],
          spaceId: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }],
          serialNumber: [{
            required: true,
            validator: this.validatePass,
            trigger: 'blur'
          }],
          hotWorkUnit: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }],
          hotLevel: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }],
          productionDepartment: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }],
          hotTime: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }],
          supervisionDepartment: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }],
          weather: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'blur'
          }],
          remark: [{
            required: false,
            message: '必填项不能为空',
            trigger: 'blur'
          }],
          hotWorkUserId: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'change'
          }],
          productionRunUserId: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'change'
          }],
          hotSupervisionUserId: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'change'
          }],
          hotCommandUserId: [{
            required: true,
            message: '必填项不能为空',
            trigger: 'change'
          }]
        },
        entMemberData: [],
        projectData: [],
        enterpriseData: [],
        hotPlanObj: {},
        entAdminData: []
      }
    },
    created() {
      this.getEnterpriseData()
      this.getEntMemberData()
    },
    methods: {
      // 企业
      getEnterpriseData() {
        let params = {
          d: {
            page: this.maxPageSize
          }
        }
        postHandle(`/api/v1/welder-sbtx-second/enterprise-comb/list`, params).then(res => {
          this.enterpriseData = res.result
        })
      },
      // 项目
      getAllProjectData() {
        let params = {
          d: {
            c: [{
              key: 'entSpaceId',
              value: this.obj.entSpaceId
            }],
            page: this.maxPageSize
          }
        }
        postHandle(`/api/v1/welder-sbtx-second/project-info-comb/list`, params).then(res => {
          this.projectData = res.result
        })
      },

      // 集团成员
      getEntMemberData() {
        let params = {
          d: {
            page: this.maxPageSize
          }
        }
        postHandle(`/api/v1/welder-uac-second/user-information-comb/get-ent-member`, params).then(res => {
          this.entMemberData = res.result
        })
      },
      // 详情
      getObjectId({
        id
      }) {
        postHandle(`/api/v1/welder-sbtx-second/inspection-comb/show/${id}`).then(res => {
          this.obj = res.result
          this.getAllProjectData()
          this.getEntAdminData()
        })
      },
      projectInfoHandle() {
        let {
          spaceId
        } = this.obj
        let obj
        if (spaceId) {
          obj = this.projectData.find(item => item.spaceId === spaceId)
          if (obj) {
            this.hotPlanObj = obj
          }
        }
      },
      submitForm() {
        this.$refs['ruleForm'].validate(valid => {
          if (valid) {
            let url
            this.projectInfoHandle()
            if (this.obj.id) {
              url = `/api/v1/welder-sbtx-second/inspection-comb/update/${this.obj.id}`
            } else {
              url = `/api/v1/welder-sbtx-second/inspection-comb/create`
            }
            postHandle(url, {
              obj: this.obj
            }).then(res => {
              this.$message.success('成功')
              this.dialog = false
              this.$emit('refreshTable')
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      closeHandle() {

        setTimeout(() => {
          this.obj = {}
          this.$refs['ruleForm'].resetFields()
        })
      },
      viewHotPlan() {
        if (!this.obj.spaceId) {
          this.$message.error('请选择项目')
          return
        }
        this.projectInfoHandle()
        let {
          hotId,
          hotProgramInfo
        } = this.hotPlanObj
        if (!hotId) {
          this.$message.error('暂无动火方案')
          return
        }

        window.open(process.env.VUE_APP_FILE_PATH + hotProgramInfo.fileUrl)

        // if (hotProgramInfo) {
        //   this.$refs['hotPlanTable'].obj = hotProgramInfo
        // }
        // this.$refs['hotPlanTable'].dialog = true
      },
      entChange() {
        this.projectData = []
        this.obj.spaceId = null
        this.getAllProjectData()
        this.getEntAdminData()
      },
      // 企业负责人
      getEntAdminData() {
        postHandle(`/api/v1/welder-uac-second/space-user-member-role-comb/get-ent-admin/${this.obj.entSpaceId}`).then(
          res => {
            this.entAdminData = res.result
          })
      },

      validatePass(rule, value, callback) {
        if (value) {
          let params = {
            d: {
              filters: [{
                field: 'serialNumber',
                operator: 'EQUALS',
                value
              }]
            }
          }
          if (this.title === '编辑') {
            params.d.filters.push({
              field: 'id',
              operator: 'NE',
              value: this.obj.id
            })
          }
          postHandle(`/api/v1/welder-sbtx-second/inspection/exists`, params).then(res => {
            if (res.result) {
              callback(new Error('编号已存在'))
            } else {
              callback()
            }
          })
        } else {
          callback(new Error('必填项不能为空'))
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  li {
    list-style: none;
  }

  .itemCon {
    border: 1px solid #ccc;
    padding: 10px;
  }

  .smallTitle {
    font-weight: bold;
    font-size: 16px;
  }
</style>